<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="事故名称" prop="sgsjName">
        <el-input v-model="queryParams.sgsjName" placeholder="请输入事故名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>


      <el-form-item label="发生时间" prop="sgsjHappen">
        <el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.sgsjHappen" type="date"
          value-format="yyyy-MM-dd" placeholder="选择发生时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['anquan:sgsj:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['anquan:sgsj:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['anquan:sgsj:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['anquan:sgsj:export']">导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="sgsjList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="更新日期" align="center" prop="sgsjId" /> -->
      <el-table-column label="事故名称" align="center" prop="sgsjName" />
      <el-table-column label="发生地点" align="center" prop="sgsjAddress" />
      <el-table-column label="事件状态" align="center" prop="sgsjStatus" :formatter="sgsjStatusFormat" />
      <!-- <el-table-column label="事故类别" align="center" prop="sgsjType" :formatter="sgsjTypeFormat" /> -->
      <el-table-column label="发生时间" align="center" prop="sgsjHappen" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sgsjHappen, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="事故等级" align="center" prop="sgsjLevel" :formatter="sgsjLevelFormat" /> -->
      <el-table-column label="上报时间" align="center" prop="sgsjReporttime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sgsjReporttime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="举报人" align="center" prop="sgsjInformant" />
      <el-table-column label="上级" align="center" prop="sgsjSuperior" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['anquan:sgsj:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['anquan:sgsj:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改事故事件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="事故名称" prop="sgsjName">
          <el-input v-model="form.sgsjName" placeholder="请输入事故名称" />
        </el-form-item>
        <el-form-item label="发生地点" prop="sgsjAddress">
          <el-input v-model="form.sgsjAddress" placeholder="请输入发生地点" />
        </el-form-item>
        <el-form-item label="事件状态">
          <el-select v-model="form.sgsjStatus" placeholder="请选择事件状态">
            <el-option v-for="dict in sgsjStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="事故类别">
          <el-select v-model="form.sgsjType" placeholder="请选择事故类别">
            <el-option v-for="dict in sgsjTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="经济损失" prop="sgsjEconomicloss">
          <el-input v-model="form.sgsjEconomicloss" placeholder="请输入经济损失" />
        </el-form-item>
        <el-form-item label="发生时间" prop="sgsjHappen">
          <el-date-picker clearable size="small" style="width: 200px" v-model="form.sgsjHappen" type="date"
            value-format="yyyy-MM-dd" placeholder="选择发生时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="责任部门" prop="sgsjDepartment">
          <el-input v-model="form.sgsjDepartment" placeholder="请输入责任部门" />
        </el-form-item>
        <el-form-item label="事故等级">
          <el-select v-model="form.sgsjLevel" placeholder="请选择事故等级">
            <el-option v-for="dict in sgsjLevelOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="事故经过" prop="sgsjPass">
          <el-input v-model="form.sgsjPass" placeholder="请输入事故经过" />
        </el-form-item>
        <el-form-item label="上报时间" prop="sgsjReporttime">
          <el-date-picker clearable size="small" style="width: 200px" v-model="form.sgsjReporttime" type="date"
            value-format="yyyy-MM-dd" placeholder="选择上报时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="举报人" prop="sgsjInformant">
          <el-input v-model="form.sgsjInformant" placeholder="请输入举报人" />
        </el-form-item>
        <el-form-item label="举报人手机号" prop="sgsjInformantphone">
          <el-input v-model="form.sgsjInformantphone" placeholder="请输入举报人手机号" />
        </el-form-item>
        <el-form-item label="上级" prop="sgsjSuperior">
          <el-input v-model="form.sgsjSuperior" placeholder="请输入上级" />
        </el-form-item>
        <el-form-item label="伤亡表id" prop="casualtyId">
          <el-input v-model="form.casualtyId" placeholder="请输入伤亡表id" />
        </el-form-item>
        <el-form-item label="调查组织单位" prop="sgsjUnit">
          <el-input v-model="form.sgsjUnit" placeholder="请输入调查组织单位" />
        </el-form-item>
        <el-form-item label="调查组织成员" prop="sgsjMember">
          <el-input v-model="form.sgsjMember" placeholder="请输入调查组织成员" />
        </el-form-item>
        <el-form-item label="事故原因分析" prop="sgsjReason">
          <el-input v-model="form.sgsjReason" placeholder="请输入事故原因分析" />
        </el-form-item>
        <el-form-item label="是否关联隐患" prop="hiddendanger">
          <el-input v-model="form.hiddendanger" placeholder="请输入是否关联隐患" />
        </el-form-item>
        <el-form-item label="相关责任处理认定" prop="sgsjCognizance">
          <el-input v-model="form.sgsjCognizance" placeholder="请输入相关责任处理认定" />
        </el-form-item>
        <el-form-item label="事故教育培建议" prop="sgsjEducation">
          <el-input v-model="form.sgsjEducation" placeholder="请输入事故教育培建议" />
        </el-form-item>
        <el-form-item label="事故整改措施建议" prop="sgsjSuggest">
          <el-input v-model="form.sgsjSuggest" placeholder="请输入事故整改措施建议" />
        </el-form-item>
        <el-form-item label="事故整改措施建议" prop="other">
          <el-input v-model="form.other" placeholder="请输入事故整改措施建议" />
        </el-form-item>
        <el-form-item label="事故整改措施建议" prop="other1">
          <el-input v-model="form.other1" placeholder="请输入事故整改措施建议" />
        </el-form-item>
        <el-form-item label="事故整改措施建议" prop="other2">
          <el-input v-model="form.other2" placeholder="请输入事故整改措施建议" />
        </el-form-item>
        <el-form-item label="事故整改措施建议" prop="other3">
          <el-input v-model="form.other3" placeholder="请输入事故整改措施建议" />
        </el-form-item>
        <el-form-item label="事故整改措施建议" prop="other4">
          <el-input v-model="form.other4" placeholder="请输入事故整改措施建议" />
        </el-form-item>
        <el-form-item label="事故整改措施建议" prop="other5">
          <el-input v-model="form.other5" placeholder="请输入事故整改措施建议" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listSgsj, getSgsj, delSgsj, addSgsj, updateSgsj, exportSgsj } from "@/api/anquan/sggl/sgsj/wsxsgsjgl";

  export default {
    name: "Sgsj",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 事故事件表格数据
        sgsjList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 事件状态字典
        sgsjStatusOptions: [],
        // 事故类别字典
        sgsjTypeOptions: [],
        // 事故等级字典
        sgsjLevelOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          sgsjName: undefined,
          sgsjAddress: undefined,
          sgsjStatus: undefined,
          sgsjType: undefined,
          sgsjEconomicloss: undefined,
          sgsjHappen: undefined,
          sgsjDepartment: undefined,
          sgsjLevel: undefined,
          sgsjPass: undefined,
          sgsjReporttime: undefined,
          sgsjInformant: undefined,
          sgsjInformantphone: undefined,
          sgsjSuperior: undefined,
          casualtyId: undefined,
          sgsjUnit: undefined,
          sgsjMember: undefined,
          sgsjReason: undefined,
          hiddendanger: undefined,
          sgsjCognizance: undefined,
          sgsjEducation: undefined,
          sgsjSuggest: undefined,
          createtime: undefined,
          updatetime: undefined,
          other: undefined,
          other1: undefined,
          other2: undefined,
          other3: undefined,
          other4: undefined,
          other5: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("s_sgsj_status").then(response => {
        this.sgsjStatusOptions = response.data;
      });
      this.getDicts("s_sgsj_type").then(response => {
        this.sgsjTypeOptions = response.data;
      });
      this.getDicts("s_sgsj_level").then(response => {
        this.sgsjLevelOptions = response.data;
      });
    },
    methods: {
      /** 查询事故事件列表 */
      getList() {
        this.loading = true;
        listSgsj(this.queryParams).then(response => {
          this.sgsjList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 事件状态字典翻译
      sgsjStatusFormat(row, column) {
        return this.selectDictLabel(this.sgsjStatusOptions, row.sgsjStatus);
      },
      // 事故类别字典翻译
      sgsjTypeFormat(row, column) {
        return this.selectDictLabel(this.sgsjTypeOptions, row.sgsjType);
      },
      // 事故等级字典翻译
      sgsjLevelFormat(row, column) {
        return this.selectDictLabel(this.sgsjLevelOptions, row.sgsjLevel);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          sgsjId: undefined,
          sgsjName: undefined,
          sgsjAddress: undefined,
          sgsjStatus: undefined,
          sgsjType: undefined,
          sgsjEconomicloss: undefined,
          sgsjHappen: undefined,
          sgsjDepartment: undefined,
          sgsjLevel: undefined,
          sgsjPass: undefined,
          sgsjReporttime: undefined,
          sgsjInformant: undefined,
          sgsjInformantphone: undefined,
          sgsjSuperior: undefined,
          casualtyId: undefined,
          sgsjUnit: undefined,
          sgsjMember: undefined,
          sgsjReason: undefined,
          hiddendanger: undefined,
          sgsjCognizance: undefined,
          sgsjEducation: undefined,
          sgsjSuggest: undefined,
          createtime: undefined,
          updatetime: undefined,
          other: undefined,
          other1: undefined,
          other2: undefined,
          other3: undefined,
          other4: undefined,
          other5: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.sgsjId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加事故事件";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const sgsjId = row.sgsjId || this.ids
        getSgsj(sgsjId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改事故事件";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.sgsjId != undefined) {
              updateSgsj(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addSgsj(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const sgsjIds = row.sgsjId || this.ids;
        this.$confirm('是否确认删除事故事件编号为"' + sgsjIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delSgsj(sgsjIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () { });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有事故事件数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportSgsj(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () { });
      }
    }
  };
</script>